package com.wugui.datax.admin.tool.query;

import com.wugui.datax.admin.entity.JobDatasource;
import com.wugui.datax.admin.util.JdbcConstants;
import com.wugui.datax.admin.util.RdbmsException;

import java.sql.SQLException;

/**
 * 工具类，获取单例实体
 *
 * @author zhouhongfa@gz-yibo.com @ClassName QueryToolFactory @Version 1.0
 * @since 2019/7/18 9:36
 */
public class QueryToolFactory {

  public static BaseQueryTool getByDbType(JobDatasource jobDatasource) {
    // 获取dbType
    String datasource = jobDatasource.getDatasource();
    if (JdbcConstants.MYSQL.equals(datasource)) {
      return getMySQLQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.ORACLE.equals(datasource)) {
      return getOracleQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.POSTGRESQL.equals(datasource)) {
      return getPostgresqlQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.SQL_SERVER.equals(datasource)) {
      return getSqlserverQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.HIVE.equals(datasource)) {
      return getHiveQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.CLICKHOUSE.equals(datasource)) {
      return getClickHouseQueryToolInstance(jobDatasource);
    } else if (JdbcConstants.HBASE20XSQL.equals(datasource)) {
      return getHbase20XsqlQueryToolQueryToolInstance(jobDatasource);
    }
    throw new UnsupportedOperationException("找不到该类型: ".concat(datasource));
  }

  private static BaseQueryTool getMySQLQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new MySQLQueryTool(jdbcDatasource);
    } catch (Exception e) {
      throw RdbmsException.asConnException(
          JdbcConstants.MYSQL,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static BaseQueryTool getOracleQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new OracleQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.ORACLE,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static BaseQueryTool getPostgresqlQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new PostgresqlQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.POSTGRESQL,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static BaseQueryTool getSqlserverQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new SqlServerQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.SQL_SERVER,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static BaseQueryTool getHiveQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new HiveQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.HIVE,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static BaseQueryTool getClickHouseQueryToolInstance(JobDatasource jdbcDatasource) {
    try {
      return new ClickHouseQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.CLICKHOUSE,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }

  private static Hbase20XsqlQueryTool getHbase20XsqlQueryToolQueryToolInstance(
      JobDatasource jdbcDatasource) {
    try {
      return new Hbase20XsqlQueryTool(jdbcDatasource);
    } catch (SQLException e) {
      throw RdbmsException.asConnException(
          JdbcConstants.HBASE20XSQL,
          e,
          jdbcDatasource.getJdbcUsername(),
          jdbcDatasource.getDatasourceName());
    }
  }
}
